package com.cn.wanxi.thread.threadtwo;

public class Consumer extends Thread {

    /**
     * 1.while(true)
     * 2.synchronized ()
     * 3.判断是否拿完，如果拿完就结束，然后等待
     * 4.如果没有拿完，就继续拿
     */
    public  void run() {
        while (true) {
            synchronized (WareHouse.object) {
                System.out.println(WareHouse.count + "Consumer");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (WareHouse.count == 0) {

                    System.out.println("仓库没有货物");
                    try {
                        WareHouse.object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else {
                    WareHouse.object.notifyAll();
                    WareHouse.count--;

                }
            }

//            synchronized (WareHouse.object) {
//                if (WareHouse.count == 0) {
//                    break;
//                } else {
//                    if (WareHouse.isNull) {
//                        System.out.println("消费者可以拿产品了");
//
//                        WareHouse.isNull = false;
//                        WareHouse.object.notifyAll();
//                        WareHouse.count--;
//                    } else {
////                        try {
////                            WareHouse.object.wait();
////                        } catch (InterruptedException e) {
////                            e.printStackTrace();
////                        }
//                    }
//                }
//            }
        }
    }
}